int ledPin = 13; // "L" LED on Arduino
int readpin = 9; // From switch's voltage divider
int val = 0;
unsigned long duration; // To time the pulse

void setup() {
  
  pinMode(ledPin, OUTPUT); // Write out to LED
  pinMode(readpin, INPUT); // pin 9 will read in
  Serial.begin(115200);
  // Serial.begin(9600); // For Arduino Serial Monitor
}

void loop() {
  val = digitalRead(readpin);
  duration = 0;
  while( val == HIGH && duration <= 1000 ) // Measure Pulse - Max 1s, Flash LED at 100Hz
  {
    digitalWrite(ledPin, HIGH);
    duration += 10; // 10 ms
    delay(10);
    val = digitalRead(readpin);
    digitalWrite(ledPin, LOW);
  }
  if( duration < 1000 ) // Page Forward - Flash LED once to indicate
  {
    Serial.print("pf");
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
  }
  else // Page Back - Flash LED Twice to indicate
  {
    Serial.print("pb");
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
  }
  delay(100);
}
